Ext.define('NF.view.farm.LivestockSearchForm', {
    extend: 'Ext.form.Panel',
    alias:'widget.livestocksearchform',
    frame:true,
    url:'livestock/searchLivestock.json',
    height: 30,
    padding: '5 0 0 0',
    width: 395,
    layout: {
        type: 'column'
    },

    initComponent: function() {
        var me = this;
        me.createArrayStore = function () {
            var myData = [['Id'],['Given name']];
            var store = Ext.create('Ext.data.ArrayStore',{
                fields: [
                   {name: 'type'}
                ],
                data:myData
            });
            return store;
        };
        Ext.applyIf(me, {
            items: [
                {
                    xtype: 'combobox',
                    fieldLabel: 'Search By',
                    displayField: 'type',
                    name:'searchBy',
                    labelPad: 1,
                    mode:'local',
                    labelSeparator: ' ',
                    labelWidth: 65,
                    editable: false,
                    forceSelection: true,
                    store:me.createArrayStore()
                },
                {
                    xtype: 'textfield',
                    name:'value',
                    padding: '0 10 0 10',
                    allowBlank: false
                },
                {
                    xtype: 'button',
                    text: 'Go',
                    formBind: true,
                    handler:function() {
                        var self = this;
                        var form = this.up('form').getForm(); // get the basic form
                        if (form.isValid()) { // make sure the form contains valid data before submitting
                            var values = form.getValues()['searchBy'] + form.getValues()['value'];
                            var farmTab = Ext.getCmp('farmTabsId');
                            farmTab.addOrActivateTab({
                                xtype:'livestocksearchpanel',
                                itemId:Ext.id('livestockSearchPanelId' + values),
                                context:{'searchBy':form.getValues()['searchBy'],'value':form.getValues()['value']}
                            });
                        } else { // display error alert if the data is invalid
                            Ext.Msg.alert('Invalid Data', 'Please correct form errors.')
                        }
                    }
                }
            ]
        });

        me.callParent(arguments);
    }
});